package com.dlss.scjp;

import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;

import android.content.res.Resources;
import android.util.Log;

public class FileReader 
{
	public List<Question> readFile(InputStream is)
	{
		Log.i("XXXXXXX", "Reading file");
		List<Question> questions = null;
		// Resources res = this.getResources() ;

        try {
            
            //TunnelDef.readTunnels(is) ;
            
            questions = getQuestions(is);
            is.close() ;        	
        }
        catch( IOException ioe ) {
        	Log.e("FileReader", "declerations failed with error: "+ioe) ;
        }
        
        return questions;

	}
	
	private List<Question> getQuestions(InputStream is) throws IOException
	{
		List<Question> questions = new ArrayList<Question>();
		
		try
		{
			
			
			DataInputStream in = new DataInputStream(is);
	        BufferedReader br = new BufferedReader(new InputStreamReader(in));
		    String strLine;
		    //Read File Line By Line
		    while ((strLine = br.readLine()) != null)   
		    {
		    	//Log.e("FileReader", strLine);	
		    	if (strLine.startsWith("QUESTION"))
		    	{
		    		Question question = new Question();
			    	addQuestion(question, br);
					addOptions(question, br);
					addAnswer(question, br);
					questions.add(question);
					
		    	}
		    }
		    //Close the input stream
		    in.close();
		}
		catch(Exception e)
		{
			Log.e("FileReader", "Adding QUESTION",e);	
		}
		
		
		return questions;
	}
	
	private void addQuestion(Question question,BufferedReader br) throws IOException
	{
		String strLine;
		
		while ((strLine = br.readLine()) != null)    
	    {
	    	if (strLine.equalsIgnoreCase("OPTIONS"))
	    	{
		    	break;		
	    	}
	    	else
	    	{
	    		question.addLineOfQuestion(strLine);
	    	}
	    }

	}
	
	private void addOptions(Question question, BufferedReader br) throws IOException
	{
		int noOfOptions = Integer.parseInt(br.readLine());
		question.setNoOfOptions(noOfOptions);
		// there will always be at least 2 options
		question.setOption1(br.readLine());
		question.setOption2(br.readLine());
		if (noOfOptions >= 3)
		{
			question.setOption3(br.readLine());
		}
		if (noOfOptions >= 4)
		{
			question.setOption4(br.readLine());
		}
		if (noOfOptions == 5)
		{
			question.setOption5(br.readLine());
		}
	}
	
	private void addAnswer(Question question, BufferedReader br) throws IOException
	{	
		
		String strLine;
		String answer = new String();
		
		question.setOptionNumberAnswer(Integer.parseInt(br.readLine().replaceAll("ANSWER ", "")));
				
		while ((strLine = br.readLine()) != null)    
	    {
	    	if (strLine.equalsIgnoreCase("END QUESTION"))
	    	{
		    	break;		
	    	}
	    	else
	    	{
	    		question.addLineOfAnswer(strLine);

	    	}
	    }
	}
}
